<?php

namespace addons\shopro\controller;

use think\Config;

class Turntable extends Common
{
    protected $noNeedLogin = ['lists'];
    protected $noNeedRight = ['*'];

    public function lists()
    {
        $list=db('turntable')->order('weigh desc')->select();
        $this->success('',$list);
    }
    public function prizeDraw()
    {
        $user = $this->auth;

        $where['user_id']=$user->id;
        $where['createtime']=['>',strtotime(date('Y-m-d 0:0:0'))];
        $count=db('turntable_log')->where($where)->count();
        $prize_draw_num=Config::get('site.prize_draw_num');
        if($count>=$prize_draw_num){
            $this->error('今日抽奖次数已用完');
        }

        $list=db('turntable')->order('weigh desc')->select();
        foreach ($list as $v) {
            $max_arr[]=$v['probability'];
        }
        $randomNum          = random_int(0, array_sum($max_arr));
        $currentProbability = 0;
        foreach ($list as $v) {

            $currentProbability += $v['probability'];
            if ($randomNum <= $currentProbability) {
                $v['currentProbability'] = $currentProbability;
                $v['randomNum']          = $randomNum;
                $draw                        = $v;
                break;
            }
        }
        $rdata['id']=$draw['id'];
        $rdata['title']=$draw['title'];
        $rdata['score']=$draw['score'];


        $log_data['user_id']=$user->id;
        $log_data['score']=$draw['score'];
        $log_data['createtime']=time();
        db('turntable_log')->insert($log_data);

        $score_log['user_id']=$user->id;
        $score_log['score']=$draw['score'];
        $score_log['before']=$user->score;
        $score_log['after']=$user->score+$draw['score'];
        $score_log['memo']='大转盘抽奖获得积分';
        $score_log['createtime']=time();
        db('user_score_log')->insert($score_log);

        $s_u_w_log['user_id']=$user->id;
        $s_u_w_log['type']='score';
        $s_u_w_log['event']='activity';
        $s_u_w_log['amount']=$draw['score'];
        $s_u_w_log['before']=$user->score;
        $s_u_w_log['after']=$user->score+$draw['score'];
        $s_u_w_log['memo']='大转盘抽奖获得积分';
        $s_u_w_log['oper_type']='user';
        $s_u_w_log['oper_id']=$user->id;
        $s_u_w_log['createtime']=time();
        db('shopro_user_wallet_log')->insert($s_u_w_log);

        db('user')->where(['id'=>$user->id])->update(['score'=>$score_log['after']]);

        $this->success('',$rdata);
    }
    public function getPoints()
    {
        $user = $this->auth;
        $qrcode_id = $this->request->param('qrcode_id');
        $qrcode = db('qrcode')->where(['id'=>$qrcode_id,'status'=>1])->find();
        if(!$qrcode){
            $this->error('二维码不存在');
        }
        $data['status']=2;
        $data['user_id']=$user->id;
        $data['receivetime']=time();
        db('qrcode')->where(['id'=>$qrcode_id])->update($data);


        $score_log['user_id']=$user->id;
        $score_log['score']=$qrcode['score'];
        $score_log['before']=$user->score;
        $score_log['after']=$user->score+$qrcode['score'];
        $score_log['memo']='扫码获得积分';
        $score_log['createtime']=time();
        db('user_score_log')->insert($score_log);

        $s_u_w_log['user_id']=$user->id;
        $s_u_w_log['type']='score';
        $s_u_w_log['event']='activity';
        $s_u_w_log['amount']=$qrcode['score'];
        $s_u_w_log['before']=$user->score;
        $s_u_w_log['after']=$user->score+$qrcode['score'];
        $s_u_w_log['memo']='扫码获得积分';
        $s_u_w_log['oper_type']='user';
        $s_u_w_log['oper_id']=$user->id;
        $s_u_w_log['createtime']=time();
        db('shopro_user_wallet_log')->insert($s_u_w_log);

        db('user')->where(['id'=>$user->id])->update(['score'=>$score_log['after']]);


        $this->success('',$qrcode);

    }
}
